package mypackage3;

import javax.script.ScriptContext;
import java.lang.reflect.AccessFlag;
import java.util.Scanner;
/*键盘录入一个字符串，要求1:长度为小于等于9要求2:只能是数字
将内容变成罗马数字
下面是阿拉伯数字跟罗马数字的对比关系:
I一1、II一2、III一3、Ⅳ一4、V—5、VI一6、VII一7、VIII—8、IX—9注意点:
罗马数字里面是没有e的
如果键盘录入的数字包含e，可以变成""(长度为e的字符串)
*/

public class ALaBoNumber {
    static String[] number={"0","I","II","III","IV","V","VI","VII","VIII","IX","X"};
    public static void main(String[] args) {
        fun1();
        //fun2();
        fun3();
    }
    public static void fun1(){
        Scanner sc=new Scanner(System.in);
        while(true){
            System.out.println("请输入一个0-999,999,999的数字");
            int num=sc.nextInt();
            if(num<0||num>999999999){
                System.out.println("输入有误");
                continue;
            }
            int temp=num;
            StringBuilder sb=new StringBuilder();
            while(temp>0){
                sb.insert(0,number[temp%10]+" ");
                temp/=10;
            }
            System.out.println(sb);
            break;
        }
    }
    public static void fun2(){
        Scanner sc=new Scanner(System.in);
        while(true){
            System.out.println("请输入一个0-999,999,999的数字");
            int num=sc.nextInt();
            if(num<0||num>999999999){
                System.out.println("输入有误");
                continue;
            }
            int temp=num;
            StringBuilder sb=new StringBuilder();
            while(temp>0){
                sb.insert(0,getNumberString(temp%10));
                sb.append(" ");
                temp/=10;
            }
            System.out.println(sb);
            break;
        }
    }
    public static String getNumberString(int num){
        String str=switch (num){
            case 0->"0 ";
            case 1->"I ";
            case 2->"II ";
            case 3->"III ";
            case 4->"IV ";
            case 5->"V ";
            case 6->"VI ";
            case 7->"VII ";
            case 8->"VIII ";
            case 9->"IX ";
            default -> "0 ";
        };
        return str;
    }
    public static void fun3(){
        Scanner sc=new Scanner(System.in);
        while(true) {
            System.out.println("请输入一个罗马数字的字符串");
            String strnum = sc.nextLine();
            int len=strnum.length();
            int fa=check(strnum,len);
            if(fa==-1){
                System.out.println("输入错误");
                continue;
            }
            StringBuilder sb=new StringBuilder();
            int cnt=0;
            int[] nums=new int[fa];
            boolean flag=true;
            for(int i=0;i<strnum.length();++i){
                char c=strnum.charAt(i);
                if(c==' '){
                    int num=getnum(sb);
                    if(num==-1){
                        flag=false;
                        break;
                    }
                    nums[cnt]=num;
                    sb=new StringBuilder();
                    ++cnt;
                }else{
                    sb.append(c);
                }
            }
            if(flag==false){
                System.out.println("输入错误");
                continue;
            }
            System.out.println("转换成对应的数字为:");
            for (int i = 0; i < cnt; i++) {
                System.out.print(nums[i]+" ");
            }
            System.out.println();
            break;
        }
    }
    public static int getnum(StringBuilder sb){
        for(int i=0;i<10;++i){
            if(sb.toString().equals(number[i])){
                return i;
            }
        }
        return -1;
    }
    public static int check(String str,int len){
        int cnt=1;
        for (int i = 0; i < len; i++) {
            char c=str.charAt(i);
            if(c!='I' && c!=' ' && c!='V' && c!='X'){
                return -1;
            }
            if(c==' '){
                ++cnt;
            }
        }
        return cnt;
    }
}
